import { listMyClassInfoVoByPageUsingPost } from '@/services/backend/classInfoController';
import { listUserClassRelationVoByPageUsingPost } from '@/services/backend/userClassRelationController';
import type { ActionType, ProColumns } from '@ant-design/pro-components';
import { PageContainer, ProTable } from '@ant-design/pro-components';
import '@umijs/max';
import React, { useRef } from 'react';

const TableList: React.FC = () => {
  const actionRef = useRef<ActionType>();

  const roleEnumMap = new Map([
    ['user', '学员'],
    ['coach', '教练'],
    ['admin', '管理员'],
  ]);

  const columns: ProColumns<API.UserVO>[] = [
    {
      title: 'id',
      dataIndex: 'id',
      valueType: 'text',
      hideInForm: true,
    },
    {
      title: '用户名',
      dataIndex: 'username',
      valueType: 'text',
    },
    {
      title: '所属班级',
      dataIndex: ['classInfo', 'className'],
      valueType: 'text',
    },
    {
      title: '头像',
      dataIndex: 'avatar',
      valueType: 'image',
      fieldProps: {
        width: 64,
      },
      hideInSearch: true,
    },
    {
      title: '简介',
      dataIndex: 'profile',
      valueType: 'textarea',
    },
    {
      title: '权限',
      dataIndex: 'role',
      valueEnum: roleEnumMap,
      hideInSearch: true,
    },
    {
      title: '创建时间',
      sorter: true,
      dataIndex: 'createTime',
      valueType: 'dateTime',
      hideInSearch: true,
      hideInForm: true,
    },
    {
      title: '更新时间',
      sorter: true,
      dataIndex: 'updateTime',
      valueType: 'dateTime',
      hideInSearch: true,
      hideInForm: true,
    },
  ];

  return (
    <PageContainer>
      <ProTable<API.User>
        headerTitle={'查询表格'}
        actionRef={actionRef}
        rowKey="key"
        search={{
          labelWidth: 120,
        }}
        // @ts-ignore
        request={async (params, sort, filter) => {
          const sortField = Object.keys(sort)?.[0];
          const sortOrder = sort?.[sortField] ?? undefined;

          //获取classIdList
          // @ts-ignore
          let classIdList = [];
          const res = await listMyClassInfoVoByPageUsingPost({});
          if (res.data) {
            classIdList = res?.data?.records?.map((classInfo) => classInfo.id) ?? [];
          }

          if (classIdList.length === 0) {
            return {
              success: 0,
              data: [],
              total: 0,
            };
          }

          const { data, code } = await listUserClassRelationVoByPageUsingPost({
            ...params,
            role: 'student',
            sortOrder,
            sortField,
            ...filter,
          });

          const filterData = data?.records?.filter((userClass) => {
            // @ts-ignore
            return classIdList.includes(userClass.classInfo?.id);
          });

          const newData = filterData?.map((userClass) => {
            return {
              ...userClass.user,
              classInfo: userClass.classInfo,
            };
          });

          return {
            success: code === 0,
            data: newData || [],
            total: Number(data?.total) || 0,
          };
        }}
        columns={columns}
      />
    </PageContainer>
  );
};
export default TableList;
